/****************************************************************************
**
** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "linuxcontrol.h"
#include <QtCore/QtGlobal>
#include <qcoreapplication.h>
#include <QtCore/QDebug>
#include <QtWidgets/QApplication>
#include <QtGui/QWindow>
#include <QtTest/QtTest>
#include <QtCore/QPoint>

#include "xdotool.c.h"
#include "cmd_getwindowfocus.c.h"
#include "cmd_getwindowname.c.h"
#include "cmd_getwindowpid.c.h"
#include "cmd_getwindowgeometry.c.h"
#include "cmd_search.c.h"
#include "cmd_click.c.h"
#include "cmd_getactivewindow.c.h"
#include "cmd_key.c.h"
#include "cmd_get_display_geometry.c.h"
#include "cmd_mousedown.c.h"
#include "cmd_behave.c.h"
#include "cmd_behave_screen_edge.c.h"
#include "cmd_getmouselocation.c.h"
#include "cmd_mousemove.c.h"
#include "cmd_windowclose.c.h"
#include "cmd_window_select.c.h"
#include "cmd_mousemove_relative.c.h"
#include "cmd_type.c.h"
#include "cmd_windowmap.c.h"
#include "cmd_mouseup.c.h"
#include "cmd_windowkill.c.h"
#include "cmd_windowmove.c.h"
#include "cmd_set_window.c.h"
#include "cmd_windowsize.c.h"
#include "cmd_windowraise.c.h"
#include "cmd_windowactivate.c.h"
#include "cmd_windowfocus.c.h"
#include "cmd_windowminimize.c.h"
#include "cmd_set_num_desktops.c.h"
#include "cmd_windowunmap.c.h"
#include "cmd_set_num_desktops.c.h"
#include "cmd_get_num_desktops.c.h"
#include "cmd_windowreparent.c.h"
#include "cmd_set_desktop.c.h"
#include "cmd_get_desktop.c.h"
#include "cmd_exec.c.h"
#include "cmd_set_desktop_for_window.c.h"
#include "cmd_get_desktop_for_window.c.h"
#include "cmd_get_desktop_viewport.c.h"
#include "cmd_set_desktop_viewport.c.h"
#include "cmd_sleep.c.h"



linuxcontrol::linuxcontrol()
{
       localEchoEnabled=false;

       m_iparserposition = 0;
    m_getstr = QString("");
    m_iorgx = 200;
    m_iorgy = 200;


    m_desktop = QApplication::desktop();

    m_ilbtnon = 0;
    m_irbtnon = 0;
    m_imbtnon = 0;

}
#define MOUSEEVENTF_MOVE        0x0001 /* mouse move */
#define MOUSEEVENTF_LEFTDOWN    0x0002 /* left button down */
#define MOUSEEVENTF_LEFTUP      0x0004 /* left button up */
#define MOUSEEVENTF_RIGHTDOWN   0x0008 /* right button down */
#define MOUSEEVENTF_RIGHTUP     0x0010 /* right button up */
#define MOUSEEVENTF_MIDDLEDOWN  0x0020 /* middle button down */
#define MOUSEEVENTF_MIDDLEUP    0x0040 /* middle button up */
#define MOUSEEVENTF_XDOWN       0x0080 /* x button down */
#define MOUSEEVENTF_XUP         0x0100 /* x button down */
#define MOUSEEVENTF_WHEEL       0x0800 /* wheel button rolled */
#define MOUSEEVENTF_VIRTUALDESK 0x4000 /* map to entire virtual desktop */
#define MOUSEEVENTF_ABSOLUTE    0x8000 /* absolute move */

void linuxcontrol::putData(const QByteArray &data)
{
    QString getdata ;//QString(data);
    QString strinsert=  getdata.fromLatin1(data.toStdString().c_str()); ;
    m_getstr +=  strinsert;

    QString qsgt = m_getstr.right(2);

    if(QString("\r\n")==qsgt)
    {
        QString strprocess = m_getstr;
        m_getstr = QString("");
        QStringList	alistparm  = strprocess.split( "\r\n");
        for(int i=0;i<alistparm.size();i++)
        {
            QString siglestr = alistparm[i];
            QStringList	anumparm =  siglestr.split( (","));
            if(anumparm.size()>1)
            {
                QString strx = anumparm.at(0).mid(1,anumparm.at(0).length()-1);
                QString stry = anumparm.at(1).mid(0,anumparm.at(1).length());
                int ix1 = strx.toInt();
                int iy1 = stry.toInt();


                m_iorgx +=ix1;
                m_iorgy +=iy1;
                if(ix1>5)
                m_iorgx +=(ix1>>1);
                if(iy1>5)
                m_iorgy +=(iy1>>1);
                if(ix1>8)
                m_iorgx +=(ix1>>1);
                if(iy1>8)
                m_iorgy +=(iy1>>1);
                if(ix1>16)
                m_iorgx +=(ix1>>1);
                if(iy1>16)
                m_iorgy +=(iy1>>1);


            }
            if(QString(("O"))==siglestr.mid(0,1))
            {


            }
            else if(QString(("l"))==siglestr.mid(0,1))
            {
#ifdef _win32
                mouse_event(MOUSEEVENTF_LEFTDOWN,1,1,0,0);
#endif

              m_ilbtnon = 1;
             // m_iwinid =  selectwinid();
              click(1);
              // mousepress(m_iorgx,m_iorgy);
             }
            else if(QString(("r"))==siglestr.mid(0,1))
            {
//              QTest::mousePress(m_desktop,Qt::MouseButton::RightButton);
#ifdef _win32

                mouse_event(MOUSEEVENTF_RIGHTDOWN,1,1,0,0);
#endif
             click(3);
            }
            else if(QString(("m"))==siglestr.mid(0,1))
            {
//              QTest::mousePress(m_desktop,Qt::MouseButton::MidButton);
#ifdef _win32

                mouse_event(MOUSEEVENTF_MIDDLEDOWN,1,1,0,0);
#endif
              click(2);
            }
            else if(QString(("L"))==siglestr.mid(0,1))
            {
#ifdef _win32

                mouse_event(MOUSEEVENTF_LEFTUP,1,1,0,0);
#endif

                m_ilbtnon = 0;
                //mouserelease(m_iorgx,m_iorgy);
            }
            else if(QString(("R"))==siglestr.mid(0,1))
            {
#ifdef _win32

                mouse_event(MOUSEEVENTF_RIGHTUP,1,1,0,0);
#endif
            }
            else if(QString(("M"))==siglestr.mid(0,1))
            {
#ifdef _win32

                mouse_event(MOUSEEVENTF_MIDDLEUP,1,1,0,0);
#endif
            }
            else if(QString(("w"))==siglestr.mid(0,1))
            {
              //   mouse_event(MOUSEEVENTF_WHEEL,1,1,1,0);
            }

        }

       //  mouse_event(MOUSEEVENTF_MOVE,m_iorgx,m_iorgy,0,0);
        if(m_iorgx<0)
            m_iorgx = 0;
        if(m_iorgy<0)
            m_iorgy = 0;
        if(m_iorgx>1600)
             m_iorgx = 1600;
        if(m_iorgy>1280)
              m_iorgy =1280;
        if(m_ilbtnon)
              windowmove(m_iwinid,m_iorgx,m_iorgy);

      // mouseEvent( QTest::MouseAction::MouseMove,

      mousemove(m_iorgx,m_iorgy);




    }

 //   insertPlainText(strinsert);
 //   QScrollBar *bar = verticalScrollBar();
//    bar->setValue(bar->maximum());
}

void linuxcontrol::setLocalEchoEnabled(bool set)
{
    localEchoEnabled = set;
}



void linuxcontrol::click(int itype)
{
/*
sleep 0.2
xdotool mousemove 40 40
sleep 0.2
xdotool mousemove 50 50
sleep 0.2
xdotool mousemove 100 100
sleep 0.2
xdotool mousemove 200 200

xdotool click 1

xdotool click 2
xdotool click 3
xdotool click 4
xdotool click 5
*/

  QString qstrnum = QString(QLatin1String("%1")).arg(itype);
  int argc = 3;
  char **argv=new char*[4];
  argv[0] = "xdotool";
  argv[1] = "click";
  argv[2] = (char*)qstrnum.toStdString().c_str();//"1";
  argv[3] = 0;
  xdotool_main(argc, argv);
}

int linuxcontrol::selectwinid()
{

    int argc = 2;
    char **argv=new char*[3];
    argv[0] = "xdotool";
    argv[1] = "selectwindow";
    argv[2] = 0;
   return xdotool_main(argc, argv);

}
void linuxcontrol::windowmove(int id,int ix,int iy)
{

    //xdotool windowmove $wid 100 100
    QString qstrid = QString(QLatin1String("%1")).arg(id);
    QString qstrx = QString(QLatin1String("%1")).arg(ix);
    QString qstry = QString(QLatin1String("%1")).arg(iy);
    int argc = 5;
    char **argv=new char*[6];
    argv[0] = "xdotool";
    argv[1] = "windowmove";
    argv[2] = (char*)qstrid.toStdString().c_str();//"1";
    argv[3] = (char*)qstrx.toStdString().c_str();//"1";
    argv[4] = (char*)qstry.toStdString().c_str();//"1";
    argv[5] = 0;

    xdotool_main(argc, argv);
}

void linuxcontrol::mousemove(int ix,int iy)
{

    //xdotool windowmove $wid 100 100

    QString qstrx = QString(QLatin1String("%1")).arg(ix);
    QString qstry = QString(QLatin1String("%1")).arg(iy);
    int argc = 4;
    char **argv=new char*[5];
    argv[0] = "xdotool";
    argv[1] = "mousemove";

    argv[2] = (char*)qstrx.toStdString().c_str();//"1";
    argv[3] = (char*)qstry.toStdString().c_str();//"1";
    argv[4] = 0;

    xdotool_main(argc, argv);
}



//#include <gtk/gtk.h>
//#include <gdk/gdkx.h>

//#include <xfconf/xfconf.h>
//#include <libxfce4util/libxfce4util.h>
//#include <libxfce4ui/libxfce4ui.h>


//#include <errno.h>
//#include <fcntl.h>
//#include <time.h>
//#include <unistd.h>
//#include <xorg-server.h>
//#include <list.h>
//#include <xorg/input.h>

//#include <exevents.h>
//#include <xkbsrv.h>
//#include <xf86.h>
//#include <xf86Xinput.h>
//#include <xf86_OSproc.h>
//#include <xserver-properties.h>
//#include <libinput.h>
//#include <linux/input.h>

//#include <X11/Xatom.h>

